/**
 * CalcAicCompExtTest.java
 *
 * Copyright (c) 2009 Teamnet International All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Teamnet International .
 **/
package ro.prjmanager.core.list;

import ro.prjmanager.core.annotations.Column;
import ro.prjmanager.core.annotations.List;
import ro.prjmanager.core.annotations.SelectOption;

import java.lang.reflect.Field;

public class ListClassProcessor {

    private String className;


    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }


    public ListClassProcessor(String className) {
        this.className = className;
    }

    public String getIdList() throws ClassNotFoundException, IllegalAccessException, InstantiationException {

        Class clazz = Class.forName(className);
        Object obj = clazz.newInstance();
        List list = obj.getClass().getAnnotation(ro.prjmanager.core.annotations.List.class);
        if (list != null) {
            return list.id();
        }
        return "";
    }

    public String process(String methodForFilter, String filterPair,Boolean loadAfterFilter) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        ListModel ret = new ListModel(className);
        Class clazz = Class.forName(className);
        Object obj = clazz.newInstance();
        List list = obj.getClass().getAnnotation(ro.prjmanager.core.annotations.List.class);
        if (list != null) {
            ret.addFooter(list, methodForFilter, filterPair,loadAfterFilter);
            ret.addHead(list, methodForFilter, filterPair,loadAfterFilter);
            Field[] fields = obj.getClass().getDeclaredFields();
            int p = 0;
            if (fields != null && fields.length > 0) {
                for (Field field : fields) {
                    Column col = field.getAnnotation(ro.prjmanager.core.annotations.Column.class);
                    if (col != null) {
                        if (p == 0) {
                            ret.updateFooter("#sortName#", col.index());
                        }
                        p++;
                        ret.addColName(col);
                        SelectOption selectOption = field.getAnnotation(SelectOption.class);
                        ret.addColModel(col, selectOption);
                    }
                }
            }
            ret.finishAddColName();
            ret.finishAddColModel();
        }
        return ret.toString();
    }


}
